<?php

$product_id = $_GET["id"];
if(!$product_id) header("location: product_order.php");
$product_id = (int)$product_id;

?>
<!DOCTYPE HTML>
<html>
	<head>
		<title>Product Share</title>
		<?php include '_head_init.php' ?>
	</head>
	<body>
		<?php include '_navbar.php' ?>
		<div class="container">
			<div class="row padding1">
				<span class="pages-list">
					<a href="#" class="btn btn-link page-link"></a>
				</span>
				<span>
					<a href="product_vote.php?id=<?php echo $product_id; ?>"
					 class="btn btn-link f-right">
						<span class="badge badge-info link">查看投票</span>
					</a>
					<a href="product_order.php" class="btn btn-link f-right">
						<span class="badge badge-info link">查看排序</span>
					</a>
					<a href="product_list_v2.php" class="btn btn-link f-right">
						<span class="badge badge-info link">返回列表</span>
					</a>
				</span>
			</div>
			<div class="row">
				<div class="span8">
					<table class="table table-bordered table-hover view-table">
						<caption></caption>
						<thead>
							<tr>
								<th width="120">评论人</th>
								<th width="320">评论内容</th>
								<th width="80">发表时间</th>
								<th width="100">操作</th>
							</tr>
						</thead>
						<tbody id="share-list">
							<tr>
								<td></td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="span4">
					<div id="display-share-list" 
						class="display-share-list padding1 thumbnail">
						<p><a href="#" class="btn btn-link"></a></p>
					</div>
				</div>
			</div>
			<div class="row padding1">
				<span class="pages-list">
				</span>
			</div>
		</div> <!-- end container -->
		<div style="display:none;" id="div-hide-clone">
			<span class="badge badge-info link">显示评论</span>
			<span class="badge badge-info link">取消</span>
		</div>
<script type="text/javascript">

jQuery(function() {
	var display_share_updateState = false;
	var product_id = <?php echo $product_id; ?>;
	var tr = $('#share-list tr:first').clone();
	var share_step = 50;
	var share_start_index = 1;
	var share_sum = 0;
	var page_sum = 1;

	function getShareList() {
		var serverReturn = _get(
			'/app/index.php?'
			+'d=api&c=wb_share&m=get_product_share_list'
			+'&product_id=<?php echo $product_id; ?>'
			+'&start_index='+share_start_index
			+'&step='+share_step+'');
		if(share_sum != serverReturn.sum){
			share_sum = serverReturn.sum;
			updateSharePage();
		}
		return serverReturn.share_list;
	}

	var show_share = $('#div-hide-clone span:first').clone();

	function generateShareTr(share) {
		var trClone = tr.clone();
		var show_shareClone = show_share.clone();
		show_shareClone.attr('share-id', share['share_id']);
		show_shareClone.bind('click', function(){
			if(display_share_updateState){
				return false;
			}else{
				display_share_updateState = true;
			}

			var sets = {};
			sets['share_is_display'] = 1;
			if(checkExistDisplayShareList($(this).attr("share-id"))){
				alert("已经添加过");
				display_share_updateState = false;
				return false;
			}else{
				updateDisplayShare($(this).attr("share-id"), sets);
			}

			var start_index = display_share_list.length;
			var tmp_tdList = $(this).parent().parent().find('td');
			display_share_list.splice(start_index,0, 
				{
					"share_id":$(this).attr("share-id"),
					"user_wb_screen_name":$(tmp_tdList[0]).html(),
					"share_content":$(tmp_tdList[1]).html()
				});
			updateDisplayShareList();

			alert("添加完成");
			display_share_updateState = false;
			return false;
		});
		var tdList = trClone.find('td');
		$(tdList[0]).html(share['user_wb_screen_name']);
		$(tdList[1]).html(share['share_content']);
		$(tdList[2]).html(share['share_create_time']);
		$(tdList[3]).append(show_shareClone);
		return trClone;
	}

	function updateShareGrid(){
		$('#share-list').empty();
		var shares = getShareList();
		for(var i in shares){
			$('#share-list').append( generateShareTr( shares[i] ) );
		}
	}

	var a = $('.pages-list:first a:first').clone();

	function generateShareSpan(page_number, span_page){
		$(span_page).empty();
		if(page_number == 1) return true;
		for(var j = 1; j <= page_number; j++){
			var aClone = a.clone();
			aClone.attr('page-number', j);
			aClone.bind('click', function(){
				var page_number = $(this).attr('page-number');
				var new_start_index = ( page_number - 1 ) * share_step + 1;
				if(share_start_index != new_start_index){
					share_start_index = new_start_index;
					updateShareGrid();
					showLimitPageLink(page_number);
				}
				return false;
			});
			$(aClone).html(j);
			$(aClone).hide();
			$(span_page.append(aClone));
		}
	}

	function updateSharePage(){
		page_sum = (share_sum - share_sum % share_step) / share_step + 
			( share_sum % share_step ? 1 : 0 );
		generateShareSpan(page_sum, $('.pages-list:first'));
		if(share_sum > share_step){
			generateShareSpan(page_sum, $('.pages-list:last'));
		}
	}

	function showLimitPageLink(page_number){
		var page_link_radius = 3;
		if(page_sum < 2) return true; // show none
		$(".page-link").hide();
		if(page_sum < 2 * (page_link_radius + 1)){ // few page - show all
			$(".page-link").attr("style", "display:inline;");
			return true;
		}
		if(page_number <= page_link_radius){ // first pages - show first all
			for(var i = 0; i < page_link_radius * 2 + 1; i++){
				$('.page-link[page-number="' + (i + 1) + '"]').attr("style", "display:inline;");
			}
		}else if(page_sum <= page_number + page_link_radius){ // last pages - show last all
			for(var i = 0; i < page_link_radius * 2 + 1; i++){
				$('.page-link[page-number="' + (page_sum - i) + '"]').attr("style", "display:inline;");
			}
		}else{ // show page by current page-number
			for(var i = page_number - page_link_radius; i < page_number + page_link_radius; i++){
				$('.page-link[page-number="' + i + '"]').attr("style", "display:inline;");
			}
		}
	}

	var display_share_list = null;
	var p = $('#display-share-list p:first').clone();

	function getDisplayShareList(){
		var serverReturn = _get(
			'/app/index.php?'
			+'d=api&c=wb_share&m=get_product_display_share_list'
			+'&product_id=<?php echo $product_id; ?>');
		return serverReturn.share_list;

	}

	function generateShareP(index, share){
		var pClone = p.clone();
		var a_first = pClone.find("a:first");
		a_first.attr('share-index', index);
		a_first.attr('share-id', share.share_id);
		a_first.html('<span class="badge badge-info link">取消</span>');
		$(a_first).bind("click", function(){
			var sets = {};
			sets['share_is_display'] = 0;
			updateDisplayShare(
				display_share_list[$(this).attr("share-index")].share_id, 
				sets);
			display_share_list.splice($(this).attr("share-index"),1);
			updateDisplayShareList();
			alert("成功取消");
			return false;
		});
		pClone.append(share.user_wb_screen_name
			+":"+share.share_content);
		return pClone;
	}

	function checkExistDisplayShareList(share_id){
		for(var i in display_share_list){
			if(display_share_list[i].share_id == share_id){
				return true;
			}
		}
		return false;
	}

	function updateDisplayShare(share_id, sets){
		var serverReturn = _post(
			'/app/index.php?'
			+'d=api&c=wb_share&m=set_share'
			+'&share_id='+share_id,
			sets);
		return serverReturn.share;
	}

	function updateDisplayShareList(){
		$('#display-share-list').empty();
		if(display_share_list == null){
			display_share_list = getDisplayShareList();
		}
		for(var i in display_share_list){
			$('#display-share-list').append(generateShareP(
				i, display_share_list[i]));
		}
	}

	updateShareGrid();
	showLimitPageLink(1);
	updateDisplayShareList();
});

</script>
	</body>
</html>